import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:laoba_menu/views/category/Data.dart';
import 'package:laoba_menu/views/favorite/conponents/UserType.dart';
import 'package:laoba_menu/views/home/components/Header.dart';
import '../home/components/Header.dart';
import './conponents/CookItem.dart';
import './conponents/CookUser.dart';

class Favorite extends StatefulWidget {
  @override
  FavoriteState createState() => new FavoriteState();
}

class FavoriteState extends State<Favorite> {
  List cookList = [];
  List cookUsers = [];
  Dio dio = Dio();

  Future getCookListData() async {
    final String url = 'http://127.0.0.1:7001/favorite/getAll';
    final res =
        await dio.get(url, queryParameters: {'user_id': 1, 'favorite_type': 1});
    var data = jsonDecode(res.toString());
    cookList = data['data']
        .map((i) => CategoryList.fromJson(i.cast<String, dynamic>()))
        .toList();
    print(data['data']);
    setState(() {
      getCookItemWidgetList();
    });
  }

  Future getCookUsersData() async {
    final String url = 'http://127.0.0.1:7001/favorite/getAll';
    final res =
        await dio.get(url, queryParameters: {'user_id': 1, 'favorite_type': 2});
    var data = jsonDecode(res.toString());
    cookUsers = data['data']
        .map((i) => UserType.fromJson(i.cast<String, dynamic>()))
        .toList();
    setState(() {
      getCookUserWidgetList();
    });
  }

  @override
  initState() {
    super.initState();
    getCookListData();
    getCookUsersData();
  }

  List<Widget> getCookItemWidgetList() {
    return cookList
        .map((item) => CookItem(
              name: item.name,
              cover: item.cover,
              desc: item.desc,
              id: item.id,
              favorite_count: item.favorite_count,
              create_time: item.create_time,
            ))
        .toList();
  }

  List<Widget> getCookUserWidgetList() {
    return cookUsers
        .map((item) => CookUser(
              nickname: item.nickname,
              id: item.id,
              desc: item.desc,
              cover: item.cover,
            ))
        .toList();
  }

  int currentTag = 0;

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: ListView(
        padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
        children: [
          Header(),
          Row(
            children: [
              GestureDetector(
                child: Container(
                  decoration: new BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(10.0)),
                    color: Colors.white,
                  ),
                  padding: const EdgeInsets.fromLTRB(20, 10, 20, 0),
                  child: Text("食谱",
                      style: TextStyle(
                          fontSize: this.currentTag == 0 ? 34 : 20,
                          color: Colors.black,
                          fontFamily: "Courier",
                          fontWeight: FontWeight.w500)),
                ),
                onTap: () => this.changeTab(0),
              ),
              GestureDetector(
                child: Container(
                  decoration: new BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(10.0)),
                    color: Colors.white,
                  ),
                  padding: const EdgeInsets.fromLTRB(0, 10, 20, 0),
                  child: Text("厨友",
                      style: TextStyle(
                          fontSize: this.currentTag == 1 ? 34 : 20,
                          color: Colors.black,
                          fontFamily: "Courier",
                          fontWeight: FontWeight.w500)),
                ),
                onTap: () => this.changeTab(1),
              ),
            ],
          ),
          ListView(
            shrinkWrap: true,
            physics: new NeverScrollableScrollPhysics(),
            padding: const EdgeInsets.fromLTRB(0, 0, 0, 20),
            children: this.currentTag == 0
                ? getCookItemWidgetList()
                : getCookUserWidgetList(),
          )
        ],
      ),
      color: Color.fromRGBO(255, 255, 255, 1),
    );
  }

  void changeTab(int index) {
    this.setState(() {
      this.currentTag = index;
    });
  }
}
